/*	Note : The CharacterStats class has his own custom editor (CharacterStatsEditor) 
 * 	
 *	The CharacterStats class provides functionality for health and stat tracking to the gameobject
 * 	It also contains functions for receiving damage and death
 *	
 *	The CharacterStats class should be used to access any stats the gameObject has
 *  The three main stats (Vitality, defence and attack) will be directly assigned for performance reasons
 *	Any added optional stats will be put into an array
 * 
 *	Author : Reinier
 */

public var attack : Stat;					/* A reference to the attack stat */
public var baseHealth : int = 100;			/* The Base Health*/
public var currentHealth : int = 100;		/* The current Health */
public var customStatArr : Stat[];			/* An array meant to be filled with any optional custom stats*/
public var defence : Stat;					/* A reference to the defence stat */
public var vitality : Stat;					/* A reference to the vitality stat */
public var xpValue : int = 5;				/* The experience granted to the killer when killed, only used for enemies */

private var maxHealth : int;				/* The maximum health */

/* variables used by the custom editor */
public var hasAttack : boolean = true;		/* A boolean used by the custom editor */
public var hasDefence : boolean = true;		/* A boolean used by the custom editor */
public var hasVitality : boolean = true;	/* A boolean used by the custom editor */
public var numberOfCustomStats : int = 0;	/* An int used by the custom editor */


/* On awaking the max health is set and the current health is set to its maximum */
function Awake(){
	SetMaxHealth();
	currentHealth = maxHealth;
}

/* OnDamage should be called whenever the damage should be dealt to the gameobject
 * It reduces the currenthealth by a certain amount and destroys this object when the currenthealth is reduced below zero
 * It also transfers any available experience values if this class contains an Experience component.
 */
function OnDamage(amount : int, source : GameObject) {
	
	/* We need to reduce the damage proportional to the amount of defence stats, but only if this object has defence */
	if(defence) {
		/* The formula for the modifier is modifier = 0.99^defence */
		var defenceModifier : float;
		defenceModifier = Mathf.Pow(0.99, defence.GetAmount());
		amount = amount * defenceModifier;
	}

	/* Reduce current health by amount */
	currentHealth = currentHealth - amount;
	
	/* Once the current health reaches zero the gameobject should die, what happens depends if the gameobject is the player or not */
	if(currentHealth < 0 || 0 == currentHealth) {
		if("Player" == gameObject.name) {
			/* TODO player dies -> respawn? retry? etc */
			Debug.Log("Player dies, the horror!");
		}
		else {
			/*TODO object dies -> respawn?
			  TODO loot drop */
			Destroy(gameObject);
			Debug.Log(gameObject.name + " has died, good riddance!");
		}
	
		/* When the object dies the killer should receive experience if able */
		var killerExperience = source.GetComponent(Experience);
		if(killerExperience){
			killerExperience.GainXP(xpValue);
		} 
		
	}
}

/* The UpdateStats method searched for all present stats in the CharacterStats class and calls their UpdateAmount method
 * The UpdateAmount method will set their stats according to the level inserted into this method
 */
function UpdateStats(level : int) {
	if(vitality) {
		vitality.UpdateAmount(level);
	}
	if(defence) {
		defence.UpdateAmount(level);
	}
	if(attack) {
		attack.UpdateAmount(level);
	}
	for(i = 0; i<customStatArr.length; i++) {
		if(customStatArr[i]){
			customStatArr[i].UpdateAmount(level);
		}
	}
}

/* During update the maxhealth is set and the currenthealth is capped at the maxhealth value */
function Update() {
	SetMaxHealth();
	
	if(currentHealth > maxHealth) {
		currentHealth = maxHealth;
	}
}

/* The Maxhealth function either sets the maxhealth at the basehealth value or increases
 * by 10 for each vitality point if the vitality stat is present
 */
function SetMaxHealth() {
	if(vitality) {
		maxHealth = baseHealth + 10*vitality.GetAmount();
	}
	else {
		maxHealth = baseHealth;
	}
}